package com.baymax.test.scene.demo

import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.Space
import android.widget.TextView
import androidx.annotation.RequiresApi
import com.baymax.test.scene.BaseScene
import com.baymax.test.scene.addButton
import com.baymax.test.scene.addDFView
import com.baymax.test.scene.drawable.DrawableUtils
import com.baymax.test.scene.drawable.DrawableUtils.createStateDrawable
import com.baymax.test.scene.internal.FocusIdEditeTextView
import com.baymax.test.scene.newSceneFlexContainer
import com.bytedance.scene.ktx.requireNavigationScene
import java.util.ArrayList

class FocusScene : BaseScene() {
    override fun onCreateView(p0: LayoutInflater, p1: ViewGroup, p2: Bundle?): View {
        return p1.newSceneFlexContainer {
            addButton("test edit") {
                requireNavigationScene().push(TestEditScene())
            }
            addButton("test focusGroup") {
                requireNavigationScene().push(FocusGroup())
            }
        }
    }

    class FocusGroup : BaseScene() {
        override fun onCreateView(
            p0: LayoutInflater,
            p1: ViewGroup,
            p2: Bundle?
        ): View {
            return p1.newSceneFlexContainer {
                val fr = FrameLayout(p1.context).apply {
                    addView(TextView(context).apply {
                        text = "Button"
                    })
                }
                addDFView(120, 80, fr) {
                    fr.isFocusable = true
                    fr.background = createStateDrawable(
                        DrawableUtils.createGradientDrawable(Color.GRAY, Color.GREEN, 4),
                        DrawableUtils.createGradientDrawable(Color.GRAY, Color.YELLOW, 4),
                    )
                    fr.setOnClickListener{
                        println("clicked")
                    }
                }

                addButton("setFocusInTouchMode") {
                    fr.isFocusableInTouchMode = !fr.isFocusableInTouchMode
                }
                addButton("request focus in touchMode") {
                    val f = fr.requestFocusFromTouch()
                    println("request $fr $f")
                }
                addButton("request focus") {
                    val f = fr.requestFocus()
                    println("request $fr $f")
                }
            }
        }

    }

    class TestEditScene : BaseScene() {
        override fun onCreateView(
            p0: LayoutInflater,
            p1: ViewGroup,
            p2: Bundle?
        ): View {
            return p1.newSceneFlexContainer {
                val t1 = FocusIdEditeTextView(p1.context).apply {
                    this.id = View.generateViewId()
                }
                val t2 = FocusIdEditeTextView(p1.context).apply {
                    this.id = View.generateViewId()
                }
                val t3 = FocusIdEditeTextView(p1.context).apply {
                    this.id = View.generateViewId()
                }
                val v = object : LinearLayout(p1.context) {
                    override fun addFocusables(views: ArrayList<View>?, direction: Int) {
                        super.addFocusables(views, direction)
                    }

                    override fun dispatchKeyEvent(event: KeyEvent): Boolean {
//                    if (event.action == KeyEvent.ACTION_DOWN) {
//                        if (event.keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
//                            if (findFocus() == t1) {
//                                t2.requestFocus()
//                                return true
//                            }
//                        }
//                    }
                        return super.dispatchKeyEvent(event)
                    }
                }
                addView(v)
                v.apply {
                    addDFView(100, 100, t1) {
                        nextFocusRightId = t2.id
                        nextFocusForwardId = t3.id

                    }
                    addDFView(100, 100, t2) {
                        this.setSingleLine()
                        nextFocusRightId = t3.id
                        nextFocusLeftId = t1.id
                        nextFocusDownId = t3.id
                    }
                    addDFView(100, 100, Space(p1.context))
                    addDFView(200, 200, t3) {
                        nextFocusLeftId = t2.id
                    }
                }

            }
        }

    }

}